package com.example.settings.service.impl;

import com.example.exception.LoginException;
import com.example.settings.dao.UserDao;
import com.example.settings.domain.User;
import com.example.settings.service.UserService;
import com.example.utils.DateTimeUtil;
import com.example.utils.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserServiceimpl implements UserService {

    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public User login(String loginAct, String loginPwd, String ip) throws LoginException {

        Map<String,String> map = new HashMap<String, String>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);

        User user = userDao.login(map);

        if (user == null){
            throw new LoginException("账号密码错误");
        }

        String expiretime =user.getExpireTime();
        String currenttime = DateTimeUtil.getSysTime();
        if (expiretime.compareTo(currenttime) < 0){
            throw new LoginException("账号已失效");
        }

        String lockstate = user.getLockState();
        if ("0".equals(lockstate)){
            throw new LoginException("账号已锁定");
        }

        String ips = user.getAllowIps();
        if (!ips.contains(ip)){
            throw new LoginException("IP地址受限制，请联系管理员");
        }
        return user;
    }

    public List<User> getUserList() {
        List<User> userList = userDao.getUserList();
        return userList;
    }
}
